package com.zbb.user.api;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.zbb.base.model.Result;
import com.zbb.base.util.RedisUtil;
import com.zbb.user.mapper.ZbbChartMapper;
import com.zbb.user.model.po.ZbbChart;
import com.zbb.user.service.ZbbChartService;
import lombok.extern.slf4j.Slf4j;
import lombok.val;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

import static org.apache.logging.log4j.util.Strings.isEmpty;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author itcast
 */
@Slf4j
@RestController
@RequestMapping()
public class ZbbChartController {
    public static final String CHARTKEY = "zbb:charts";

    @Autowired
    private ZbbChartService  zbbChartService;

    @Autowired
    ZbbChartMapper zbbChartMapper;

    @Autowired
    RedisUtil redisUtil;


    @GetMapping("getChart")
    public Result getChart(){
        String chartsJson = (String) redisUtil.get(CHARTKEY);
        Gson gson = new Gson();
        List<ZbbChart> zbbCharts = new ArrayList<>();
        if (!isEmpty(chartsJson)){
            List<ZbbChart> zbbChartList = gson.fromJson(chartsJson, new TypeToken<List<ZbbChart>>(){}.getType());
            return Result.ok(zbbChartList);
        }
        QueryWrapper<ZbbChart> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("status",1);
        zbbCharts = zbbChartMapper.selectList(queryWrapper);
        String zbbChartsJson = gson.toJson(zbbCharts);
        redisUtil.set(CHARTKEY,zbbChartsJson,86400);
        return Result.ok(zbbCharts);
    }
}
